import { Injectable } from "@nestjs/common";
import * as OSS from 'ali-oss'
import * as dayjs from 'dayjs'

@Injectable()
export class OssService {
    // 新增
    async getSignature() {
        const config = {
            region:'oss-cn-hangzhou',
            accessKeyId: process.env.ACCESS_KEY,
            accessKeySecret: process.env.ACCESS_KEY_SECRET,
            bucket: 'water-drop-leo-assets',
            dir: 'images/'
        };
    
        const client = new OSS(config);
        
        const date = new Date();
        date.setDate(date.getDate() + 1);
        const policy = {
            expiration: date.toISOString(), // 请求有效期
            conditions: [
                ['content-length-range', 0, 1048576000], // 设置上传文件的大小限制
            ],
        };

        //签名
        const formData = await client.calculatePostSignature(policy);
        
        //bucket域名
        const host = `https://${config.bucket}.${
            (await client.getBucketLocation()).location
        }.aliyuncs.com`.toString();
        
        //返回参数
        const params = {
            expire: dayjs().add(1, 'days').unix().toString(),
            policy: formData.policy,
            signature: formData.Signature,
            accessId: formData.OSSAccessKeyId,
            host,
            dir: 'images/',
        };

        return params;
    }
}